//
//  ArtistViewController.m
//  jinnengExam
//
//  Created by 陈洪锋 on 2023/8/29.
//  Copyright © 2023 jznrj. All rights reserved.
//

#import "ArtistViewController.h"
#import "LoginViewController.h"
#import "PointViewControllerViewController.h"
#import "ScanCodeViewController.h"
#import "UserCenterViewController.h"
#import "MBProgressHUD.h"
#import "AFHTTPRequestOperation.h"
#import "NewsDetailViewController.h"
#import "SearchViewController.h"
@interface ArtistViewController ()

@property(nonatomic) CGRect frame;
@property(nonatomic,strong) JznWebView *titleView;
@property(nonatomic, strong) WKWebViewJavascriptBridge *titleBridge;
@property(nonatomic,strong) NSString *trade;
@property(nonatomic) int page;
@end

@implementation ArtistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _trade = @"";
    _frame = self.tabBarController.tabBar.frame;
    [self initView];
    [self registerNativeFunctions];
}
-(void) viewWillAppear:(BOOL)animated{
//    self.navigationController.navigationBar.hidden =YES;
//    self.tabBarController.tabBar.hidden = NO;
//    self.tabBarController.tabBar.frame = _frame;
    [self requestPoint];
    
}
-(void) viewDidAppear:(BOOL)animated{
//    self.navigationController.navigationBar.hidden =YES;
//    self.tabBarController.tabBar.hidden = NO;
//    self.tabBarController.tabBar.frame = _frame;
   
}
-(void) initView{
    self.titleView  = [[JznWebView alloc]initWithFrame:_frame];
    [self.view addSubview:self.titleView];
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0);
        make.width.left.equalTo(self.view);
        make.top.equalTo(self.view).offset(-5);
        
    }];
    [self.titleView loadLocalFile:@"/ios_jinneng/jinnengTitle" type:@"html"];
    
    [self.view addSubview:self.jznWebView];
    [self.jznWebView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.right.equalTo(self.view);
         make.top.equalTo(self.view);
         if (@available(iOS 11.0, *)) {

             make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
         } else {
             make.bottom.equalTo(self.view);
         }
     }];
   [self.jznWebView loadLocalFile:@"/ios_jinneng/artist" type:@"html"];
    
    [self.jznWebView.scrollView.mj_header setBackgroundColor:[UIColor whiteColor]];
    self.jznWebView.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self requestLearn:self.page];
        
    }];
    
    self.jznWebView.scrollView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [self requestLearn:++self.page];
    }];
}

- (void)registerNativeFunctions {

    [self.webViewBridge registerHandler:@"tradeChange" handler:^(id data, WVJBResponseCallback responseCallback) {
        self.trade = data[@"module"];
        [self.jznWebView.scrollView.mj_header beginRefreshing];
    }];
    
    [self.webViewBridge registerHandler:@"toNewsDetailViewController" handler:^(id data, WVJBResponseCallback responseCallback) {

        dispatch_async(dispatch_get_main_queue(), ^(){
            NewsDetailViewController *newsDetailViewController = [[NewsDetailViewController alloc]init];
            [newsDetailViewController setParams:[data mj_JSONString]];
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:newsDetailViewController animated:YES];
        });
    }];
    
    [self.titleBridge registerHandler:@"toPoint" handler:^(id data, WVJBResponseCallback responseCallback) {
        
        dispatch_async(dispatch_get_main_queue(), ^(){

            PointViewControllerViewController *vc = [[PointViewControllerViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        });
        
    }];
    
    [self.titleBridge registerHandler:@"toUserCenter" handler:^(id data, WVJBResponseCallback responseCallback) {
        
        dispatch_async(dispatch_get_main_queue(), ^(){

            UserCenterViewController *vc = [[UserCenterViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        });
        
    }];
    
    [self.titleBridge registerHandler:@"toSearch" handler:^(id data, WVJBResponseCallback responseCallback) {
        
        dispatch_async(dispatch_get_main_queue(), ^(){

            SearchViewController *vc = [[SearchViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        });
        
    }];
    
    [self.titleBridge registerHandler:@"toScan" handler:^(id data, WVJBResponseCallback responseCallback) {
          
          dispatch_async(dispatch_get_main_queue(), ^(){
              NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
              if ([userDefault objectForKey:@"account"] == nil) {
                  LoginViewController *controller = [[LoginViewController alloc]init];
                  controller.tabIndex = 1;
                  controller.modalPresentationStyle = UIModalPresentationFullScreen;
                  [self presentModalViewController:controller animated:YES ];
                  return;
              }else{
                  ScanCodeViewController *learnNewsViewController = [[ScanCodeViewController alloc]init];
                  self.hidesBottomBarWhenPushed = YES;
                  [self.navigationController pushViewController:learnNewsViewController animated:YES];
              }
          });
          
      }];
}

-(void) requestLearn:(int) page{
    NSString *url = [NSString stringWithFormat:@"%@/newsView/findEmployeeNews" ,[URLUtil hostName]];
    NSDictionary *params = @{ @"employeeType":_trade,@"pageNum":[NSString stringWithFormat:@"%d",page],@"pageSize":@"12"};
    [JZNRequestUtil requestUrl:url requestParams:params success:^(DataResult *dataResult) {
        if(dataResult.code == 0){
            if (page == 1) {
                NSString *clear = @"clear()";
               
                [self.jznWebView evaluateJavaScript:clear completionHandler:^(id _Nullable data, NSError * _Nullable error) {
                    
                }];
                [self.jznWebView.scrollView.mj_header endRefreshing];
                [self.jznWebView.scrollView.mj_footer resetNoMoreData];
            }
            
            NSString *clear = [NSString stringWithFormat:@"getNewsList(%@)",[dataResult.data mj_JSONString]];
           // [self.jznWebView stringByEvaluatingJavaScriptFromString: clear];
            
            [self.jznWebView evaluateJavaScript:clear completionHandler:^(id _Nullable data, NSError * _Nullable error) {
                
            }];
            NSArray *array = dataResult.data;
            if([array count]<1){
                
                [self.jznWebView.scrollView.mj_footer endRefreshingWithNoMoreData];
            }else{
                
                [self.jznWebView.scrollView.mj_footer endRefreshing];
            }
        }else {
        }
    } failure:^(NSError *error) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    [self.jznWebView.scrollView.mj_header beginRefreshing];
    [self requestPoint];
    NSString *js = [NSString stringWithFormat:@"getUserName('%@')",self.loginDto.userName];
    [self.titleView evaluateJavaScript:js completionHandler:nil];
    
    NSString *js1 = [NSString stringWithFormat:@"getCompanyName('%@')",self.loginDto.businessCompanyName];
    [self.titleView evaluateJavaScript:js1 completionHandler:nil];
    
    [self.jznWebView.scrollView.mj_header beginRefreshing];
}

- (void)tabBarController:(UITabBarController*) tabBarController didSelectViewController:(UIViewController*)viewController{
    if (self.tabBarController.tabBar.isHidden) {
        self.tabBarController.tabBar.hidden = NO;
        self.tabBarController.tabBar.frame = _frame;
    }
}
-(void) requestPoint{
    NSString *url = [NSString stringWithFormat:@"%@/userRank/findUserScore",[URLUtil hostName]];
    NSDictionary *params = @{@"userId":[NSString stringWithFormat:@"%d",self.loginDto.userId]};
    [JZNRequestUtil requestUrl:url requestParams:params success:^(DataResult *dataResult) {
        if(dataResult.code == 0){
            NSString *js = [NSString stringWithFormat:@"getPoint(%@)",dataResult.data];
            [self.titleView evaluateJavaScript:js completionHandler:nil];
        }else {
        }
    } failure:^(NSError *error) {
        
    }];
    
    NSString *url1 = [NSString stringWithFormat:@"%@/userEntry/userLoginCount",[URLUtil hostName]];
    NSDictionary *params1 = @{};
    [JZNRequestUtil requestUrl:url1 requestParams:params1 success:^(DataResult *dataResult) {
        if(dataResult.code == 0){
            NSString *js = [NSString stringWithFormat:@"getUserCount(%@)",dataResult.data];
            [self.titleView evaluateJavaScript:js completionHandler:nil];
        }else {
        }
    } failure:^(NSError *error) {
        
    }];
}
@end

